/* CVRA city council analysis */

cd "C:\Users\Garrett.Glasgow\OneDrive - MMC\Desktop\CVRA"

log using CVRA_city_council_analysis_final.txt, text replace

/**********************/
/** Data Preparation **/
/**********************/

insheet using "CVRA_city_council_data.csv", clear

keep if treatment_cohort == 0 | treatment_cohort >= 2016
drop if city=="Vernon"

egen citynum = group(city)

destring candidates, replace force
replace candidates = seats if candidates==.

collapse (sum) seats uncontested_seats uncontested_seats_alt candidates (mean) treatment_cohort totregvoters districts pcthispanic2020 pcthispanic2010 pcthispanic2000 pctblack2020 pctblack2010 pctblack2000 pctasian2020 pctasian2010 pctasian2000 numdist seatdiff, by(citynum time)

/**************/
/** Figure 1 **/
/**************/

/* Entropy Balancing */
generate treatment = (treatment_cohort != 0)  
quietly: ebalance treatment totregvoters pcthispanic2020 pcthispanic2010 pcthispanic2000 pctblack2020 pctblack2010 pctblack2000 pctasian2020 pctasian2010 pctasian2000, generate(eweight)

/* DiD model */
quietly: jwdid uncontested_seats [weight=eweight], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)
estat event, vce(unconditional) pretrend
estat plot


/**************/
/** Figure 2 **/
/**************/

quietly: jwdid candidates [weight=eweight], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)
estat event, vce(unconditional) pretrend
estat plot

/**************/
/** Table A2 **/
/**************/

/* Entropy Balancing */
generate treatment_notyet = (districts != 0)  
quietly: ebalance treatment_notyet totregvoters pcthispanic2020 pcthispanic2010 pcthispanic2000 pctblack2020 pctblack2010 pctblack2000 pctasian2020 pctasian2010 pctasian2000, generate(eweight_notyet)

/* DiD Models */
quietly: jwdid uncontested_seats [weight=eweight], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: jwdid uncontested_seats [weight=eweight_notyet], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) 
estat simple, vce(unconditional)

quietly: jwdid uncontested_seats, ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: jwdid uncontested_seats, ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) 
estat simple, vce(unconditional)

/**************/
/** Table A3 **/
/**************/

quietly: jwdid uncontested_seats_alt [weight=eweight], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: jwdid uncontested_seats_alt [weight=eweight_notyet], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) 
estat simple, vce(unconditional)

quietly: jwdid uncontested_seats_alt, ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: jwdid uncontested_seats_alt, ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) 
estat simple, vce(unconditional)

/**************/
/** Table A5 **/
/**************/

preserve
 
drop if seatdiff > 0

quietly: ebalance treatment totregvoters pcthispanic2020 pcthispanic2010 pcthispanic2000 pctblack2020 pctblack2010 pctblack2000 pctasian2020 pctasian2010 pctasian2000, generate(eweight_diff)

quietly: jwdid uncontested_seats [weight=eweight_diff], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: ebalance treatment_notyet totregvoters pcthispanic2020 pcthispanic2010 pcthispanic2000 pctblack2020 pctblack2010 pctblack2000 pctasian2020 pctasian2010 pctasian2000, generate(eweight_notyet_diff)

quietly: jwdid uncontested_seats [weight=eweight_notyet_diff], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) 
estat simple, vce(unconditional)

quietly: jwdid uncontested_seats, ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: jwdid uncontested_seats, ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) 
estat simple, vce(unconditional)

restore

/**************/
/** Table A6 **/
/**************/

quietly: jwdid candidates [weight=eweight], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: jwdid candidates [weight=eweight_notyet], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) 
estat simple, vce(unconditional)

quietly: jwdid candidates, ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: jwdid candidates, ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) 
estat simple, vce(unconditional)


/*******************/
/** Placebo Tests **/
/*******************/

preserve

replace treatment_cohort = treatment_cohort - 4 if treatment_cohort > 0
drop if districts == 1

quietly: ebalance treatment totregvoters pcthispanic2020 pcthispanic2010 pcthispanic2000 pctblack2020 pctblack2010 pctblack2000 pctasian2020 pctasian2010 pctasian2000, generate(eweight_diff_p)

quietly: jwdid uncontested_seats [weight=eweight_diff_p], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

quietly: jwdid candidates [weight=eweight_diff], ivar(citynum) tvar(time) gvar(treatment_cohort) method(poisson) exposure(seats) never
estat simple, vce(unconditional)

restore

/*******************************/
/** Statistics For Discussion **/
/*******************************/

egen seats2024 = total(seats) if treatment_cohort!=0 & time==2024
egen seatspre2016 = total(seats) if treatment_cohort!=0 & time<2016
egen uncseats2024 = total(uncontested_seats) if treatment_cohort!=0 & time==2024
egen uncseatspre2016 = total(uncontested_seats) if treatment_cohort!=0 & time<2016
generate pct2024 = uncseats2024/seats2024
generate pctpre2016 = uncseatspre2016/seatspre2016

generate votingpop2024 = (totregvoters/numdist) * seats if treatment_cohort!=0 & time==2024
generate uncvotingpop2024 = (totregvoters/numdist) * uncontested_seats if treatment_cohort!=0 & time==2024
egen totvotingpop2024 = total(votingpop2024) if treatment_cohort!=0 & time==2024
egen totuncvotingpop2024 = total(uncvotingpop2024) if treatment_cohort!=0 & time==2024
generate pctpopunc = totuncvotingpop2024/totvotingpop2024

summarize pctpre2016 uncseatspre2016 seatspre2016 pct2024 uncseats2024 seats2024 totuncvotingpop2024 pctpopunc


log close
